﻿LG.changepassword=function(){
  if(!window.changePasswordWin){
    var changePasswordPanle=$("<form id='form_change_password'></form>");
    changePasswordPanle.ligerForm({
      fields:[{
        display:'旧密码',
        name:'oldPassword',
        type:'password',
        validate:{
          maxlength:50,
          required:true,
          messages:{
            required:'请输入密码'
          }
        }
      },{
        display:'新密码',
        name:'password',
        id:'newPassword',
        type:'password',
        validate:{
          maxlength:50,
          required:true,
          messages:{
            required:'请输入密码'
          }
        }
      },{
        display:'确认密码',
        type:'password',
        id:'validatePassword',
        validate:{
          maxlength:50,
          required:true,
          equalTo:'#newPassword',
          messages:{
            required:'请输入密码',
            equalTo:'两次密码输入不一致'
          }
        }
      }]
    });
    
    //验证
    jQuery.metadata.setType("attr","validate");
    LG.validate(changePasswordPanle);
    
    window.changePasswordWin=$.ligerDialog.open({
      width:350,
      height:'auto',
      style:'margin:10px',
      top:200,
      isResize:true,
      title:'用户修改密码',
      target:changePasswordPanle,
      buttons:[{
        text:'确定',
        onclick:function(){
          doChangePassword();
        }
      },{
        text:'取消',
        onclick:function(){
          closePasswordDialog();
        }
      }]
    });
  }else{
    window.changePasswordWin.show();
  }
  
	function closePasswordDialog(){
	  var $form=$("#form_change_password");
    window.changePasswordWin.hide();
	  //$.ligerDialog.hide();
	  $form.ligerForm().cleanInvalid();
	  $("#form_change_password")[0].reset();
	}

  function doChangePassword(){
   var $form=$("#form_change_password");
    if(changePasswordPanle.valid()){
      LG.ajax({
        url:"user/ajaxcmd.do?method=changeUserSelfPassword",
        data:$form.serialize(),
        success:function(){
          LG.showSuccess('密码修改成功');
         closePasswordDialog();
        },
        error:function(message){
          LG.showError(message);
        }
      });
    }
  }

  $('#validatePassword').keydown(function(e){
    if(e.keyCode==13){
      doChangePassword();
    }
  });
  
};